package com.wing.cabinet.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.cabinet.controller.websocket.CabinetOpenServer;
import com.wing.cabinet.dao.CheckedResultStorageDao;
import com.wing.cabinet.model.entity.CheckedResult;
import com.wing.cabinet.model.entity.CheckedResultStorage;
import com.wing.cabinet.model.request.CheckedResultStorageForm;
import com.wing.cabinet.model.response.CabinetVO;
import com.wing.cabinet.model.response.CheckedResultStorageVO;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.JsonResult;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 存贮盘验结果到柜子-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class CheckedResultStorageServiceImpl extends ServiceImpl<CheckedResultStorageDao, CheckedResultStorage> implements CheckedResultStorageService {

    @Autowired
    protected CheckedResultService checkedResultService;
    @Autowired
    private CabinetService cabinetService;

    @Override
    public int add(CheckedResultStorageForm checkedResultStorageForm) {

        //校验

        CheckedResultStorage checkedResultStorage =new CheckedResultStorage();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(CheckedResultStorage.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(checkedResultStorageForm, checkedResultStorage, copyOptions);

        return baseMapper.insert(checkedResultStorage);
    }

    @Override
    public List<CheckedResultStorageVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<CheckedResultStorageVO> pageList4app(Page page, Map<String, Object> params) {
        List<CheckedResultStorageVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public CheckedResultStorageVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    @Transactional
    public JsonResult openCabAndSave(CheckedResultStorageForm checkedResultStorageForm) {
        Long creator= JwtUtils.getLoginUserId();
        String creatorName=JwtUtils.getLoginUsername();
        String enterpriseCode=JwtUtils.getEnterpriseCode();
        if (CollectionUtils.isEmpty(checkedResultStorageForm.getResultList())){
            throw new BusinessException("请选择盘验结果");
        }
        String cabCode=checkedResultStorageForm.getCabCode();
        CabinetVO cabinetVO = cabinetService.get4appByCode(cabCode);
        String spaceCode=checkedResultStorageForm.getSpaceCode();
        try{
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("cabCode",cabCode);
            jsonObject.put("spaceCode",spaceCode);
            jsonObject.put("type","remoteOpen");
            CabinetOpenServer.sendMessage(enterpriseCode,cabinetVO.getBelongCabinet(), jsonObject.toJSONString());
        }catch (Exception e){
            log.error("open cab fail",cabCode,spaceCode,e);
            throw new BusinessException("开柜失败");
        }
        List<Long> resultIdList=new ArrayList<>();
        for (CheckedResultStorage checkedResultStorage:checkedResultStorageForm.getResultList()){
            checkedResultStorage.setCabCode(cabCode);
            checkedResultStorage.setSpaceCode(spaceCode);
            checkedResultStorage.setCabId(checkedResultStorageForm.getCabId());
            checkedResultStorage.setSpaceId(checkedResultStorageForm.getSpaceId());
            checkedResultStorage.setCreator(creator);
            checkedResultStorage.setCreatorName(creatorName);
            resultIdList.add(checkedResultStorage.getCheckedResultId());
        }
        this.saveBatch(checkedResultStorageForm.getResultList());
        CheckedResult checkedResult=new CheckedResult();
        checkedResult.setIsStorage(true);
        checkedResultService.update(checkedResult,new QueryWrapper<CheckedResult>().in("id",resultIdList));
        return JsonResult.success();
    }
}
